#!/bin/bash
#========================================================================
# Author: YSLin
# Email: 
# File Name: test.sh
# Description: 
#   
# Edit History: 
#   2010-12-08    File created.
#========================================================================
#test					true if ....

#[ string1 = string2 ]   string1 and string2 are equal
#[ string1 != string2 ]  string1 and string2 are not equal
#[ string1 \< string2 ]	string1 is lexically less than string2 (e.g. 'a' is less than 'b')
#[ string1 \> string2 ]	string1 is lexically greater than string2 (e.g. 'b' is greater than 'a')
#[ -z string ]			string is zero (e.g. a empty string)
#[ -n string ]			string is nonzero (e.g. a VAR string)
#[ -e file ]				file exists
#! [ -e file ]           file doesn't exit
#[ -f file ]				file is a file
#[ -d file ]				file is a directory
#[ -c file ]				file is a character device
#[ -b file ]				file is a block device
#[ -p file ]				file is a named pipe
#[ -s file ]				file is not empty
#[ -k file ]				file's sticky bit is set
#[ -S file ]				file is a socket
#[ -L file ]				file is a symbolic link
#[ -r file ]				file is readable by user
#[ -w file ]				file is writeable by user
#[ -x file ]				file is executeable by user
#[ -O file ]				file is owner by user
#[ -G file ]				file is group owned by a greoup 
#[ -u file ]				file has its set user ID bit set
#[ -g file ]				file has its group user ID bit set
#[ file1 -nt file2 ]		file1 is newer than file2
#[ file1 -ot file2 ]		file1 is older than file2
#[ file -ef file2 ]		file1 is another name for file2
#[ n1 -eq n2 ]			true if integer n1 = integer n2
#[ n1 -ne n2 ]			true if integer n1 <> n2     
#[ n1 -gt n2 ]			true if n1 > n2                 
#[ n1 -ge n2 ]			true if n1 >= n2
#[ n1 -lt n2 ]			true if n1 < n2
#[ n1 -le n2 ]			true if n1 <= n2

if [ -e test.sh ] || [ str1 = str2 ]; then
    echo "OK"
fi
#可以用!當作not
if ! [ -e file ] || [ str1 = str2 ]; then
    echo "file doesn't exsit or str1 = str2"
fi
#可以用括號
str1=1
str2=1
if ! ([ -e test.sh ] && [ $str1 = $str2 ]); then
    echo "file doesn't exsit or str1 = str2"
fi

if [ -e adir ] && [ -d adir ]; then
	echo "-e-d adir"
fi

# 1. 讓使用者輸入檔名，並且判斷使用者是否真的有輸入字串？
echo -e "Please input a filename, I will check the filename's type and \
permission. \n\n"
read -p "Input a filename : " filename
test -z $filename && echo "You MUST input a filename." && exit 0
# 2. 判斷檔案是否存在？若不存在則顯示訊息並結束腳本
test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit 0
# 3. 開始判斷檔案類型與屬性
test -f $filename && filetype="regulare file"
test -d $filename && filetype="directory"
test -r $filename && perm="readable"
test -w $filename && perm="$perm writable"
test -x $filename && perm="$perm executable"
# 4. 開始輸出資訊！
echo "The filename: $filename is a $filetype"
echo "And the permissions are : $perm"
